<template>
  <div class="defectLibrary flowCloumn h100">
    <div class="h100">
      <list-table-new
        @focusInput="focusInput"
        @changeInput="changeInput"
        v-bind="tableSetting"
        ref="klTable"
      ></list-table-new>
    </div>
  </div>
</template>
<script>
import {
  queryDefectList,
  queryDefectType,
  DefectRefPlus,
} from "@/api/standardManagement/defectLibrary.js";
export default {
  props: {
    selectId: {
      type: Array,
      default: () => {
        return [];
      },
    },
    disableIds: {
      type: Array,
      default: () => {
        return [];
      },
    },
    needAdd:{
      type: Boolean,
      default:true,
    }
  },
  data() {
    return {
      tableSetting: {
        radioShow: true,
        showFilterBtn: false,
        showCustomBtn: false,
        // selectionShow: true,
        showIndex: false,
        defaultSort: "parentTypeName",
        defaultSortAsc: false,
        searchObj: {
          searchMethod: (params, sortStr) => {
            this.getTableData(params, sortStr);
          },
          queryData: {
            page: 1,
            pageSize: 15,
          },
          searchArr: [
            {
              type: "select",
              label: "缺陷类别",
              model: "parentTypeName",
            },
            {
              type: "select",
              label: "缺陷子类",
              model: "typeName",
            },
            {
              type: "input",
              label: "缺陷关键字",
              model: "keyword",
              placeholder: "请输入",
            },
          ],
        },
        //过滤器的值
        filterFormData: {},
        //过滤器的默认值
        defaultFilterData: {},
        userCustomData: [],
        actionList: [
          {
            name: "前往添加",
            icon: "el-icon-plus",
            disabled: !this.$store.getters.isHaveMenuAuth("defectLibrary"), //控制按钮的禁用
            content: "缺陷库页面未授权", //禁用提示内容
            method: (row) => {
              this.goDetailPage();
            },
          },
        ],
        column: [
          {
            label: "缺陷类别",
            prop: "parentTypeName",
            sortable: "custom",
          },
          {
            label: "缺陷子类",
            prop: "typeName",
            sortable: "custom",
          },
          {
            label: "缺陷关键字",
            prop: "keyword",
            sortable: "custom",
          },
          {
            label: "类似描述",
            prop: "others",
            sortable: "custom",
          },
          {
            label: "引用次数",
            prop: "num",
            sortable: "custom",
          },
        ],
        //表格方法
        tableMethods: {
          //点击行的方法
          rowClick: (row) => {
            if (!this.disableIds.includes(row.id)) {
              this.$refs.klTable.radioSelect(row);
            }
          },
        },
        isDisabled: (row) => {
          return this.disableIds.includes(row.id);
        },
      },
      parentTypeName: "", //缺陷分类名
      typeName: "", //缺陷子类名
      others: {},
      haveAuth: null,
    };
  },
  mounted() {
    this.init();
  },
  methods: {
    //初始化入口
    init() {
      if (this.pageType == "details") {
        this.tableSetting.actionList = [];
      }
      this.$refs.klTable.searchData();
      this.getDefectType();
    },
    //表单输入框获取焦点
    focusInput(model, searchForm) {
      if (model === "typeId") {
        if (!searchForm.parentTypeName || searchForm.parentTypeName == "") {
          this.$message.warning("请先选择缺陷类别");
        }
      }
    },
    //
    changeInput(model, searchForm) {
      if (model === "parentTypeName") {
        this.getDefectChild(searchForm.parentTypeName);
      }
    },
    //获取缺陷列表
    getTableData(params, sortStr) {
      let queryData = Object.assign({}, params);
      let sort = JSON.parse(JSON.stringify(sortStr));
      sort.push({
        column: "id",
        asc: true,
      });
      if (sortStr.length > 0) {
        queryData.orders = sort;
      }
      queryDefectList(queryData).then((res) => {
        this.$set(this.tableSetting, "tableData", res.data.records);
        this.$set(this.tableSetting, "totalNumber", res.data.total);
        let data = res.data.records || [];
        /* 回显选中的数据 */
        data.map((i) => {
          if (this.selectId.includes(i.id)) {
            this.$refs.klTable.radioSelect(i);
          }
        });
      });
    },
    //获取缺陷类型
    getDefectType() {
      queryDefectType().then((res) => {
        let arr = res.data.map((item) => {
          const obj = {
            label: item.parentTypeName,
            value: item.parentTypeName,
          };
          return obj;
        });
        this.$set(this.tableSetting.searchObj.searchArr[0], "option", arr);
        // this.$set(this.formConfig.itemArr[0], "selectOption", arr);
      });
    },
    goDetailPage() {
      const { href } = this.$router.resolve({
        name: "defectLibrary",
      });
      window.open(href, "_blank");
    },
    //获取表格表单缺陷子类
    getDefectChild(name) {
      queryDefectType({ typeName: name }).then((res) => {
        let arr = res.data.map((item) => {
          const obj = {
            label: item.typeName,
            value: item.typeName,
          };
          return obj;
        });
        this.$set(this.tableSetting.searchObj.searchArr[1], "option", arr);
      });
    },
    //获取选择结果
    async getSelectData() {
      try {
        let data = this.$refs.klTable.getRadioData();
        if (this.needAdd) {
          await DefectRefPlus({ id: data.id });
        }  
        return data;
      } catch(err) {
        console.log(err);
      }
    },
  },
};
</script>
<style lang="less" scoped>
.defectLibrary {
  padding: 20px;
}
.flowCloumn {
  display: flex;
  flex-flow: column nowrap;
}
</style>
